﻿using Sharp.Infrastructure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Sharp.Domain.PlatformBoundedContext.StoragerModule.Files
{
    /// <summary>
    /// 存储器
    /// </summary>
    public class SharpStorager
    {
        private static readonly IFileStorager Cs;
        private static readonly object SyncRoot = new Object();
        private static readonly StoragerConfig ConfigInfo;
        static SharpStorager()
        {
            if (ConfigInfo == null)
            {
                lock (SyncRoot)
                {
                    if (ConfigInfo == null)
                    {
                        ConfigInfo = SerializableHelper.DeSerialize<StoragerConfig>(new FileInfo(HttpContext.Current.Server.MapPath("~\\Config\\Storager.config")));
                    }
                }
            }
            //策略模式实现算法切换

            if (ConfigInfo == null)
            {
                ConfigInfo = new StoragerConfig(); 
            }
            switch (ConfigInfo.Store)
            {
              case  "DB": 
                    Cs = new DBStorager();
            break;
                case "Local":
                    Cs = new LocalHostStorager();
                    break;
                default:
                    Cs = new DBStorager();
                    break;
            }
        }
        /// <summary>
        /// 当前存储器
        /// </summary>
        public static IFileStorager Context
        {
            get
            {
                return Cs;
            }
        }
    }
}
